package com.yonyou.brigade.order_center.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yonyou.brigade.order_center.dao.BriHotelRoomMapper;
import com.yonyou.brigade.order_center.model.BriHotelRoomDTO;
import com.yonyou.brigade.order_center.model.BriHotelRoom;
import com.yonyou.brigade.order_center.service.IBriHotelRoomService;
import com.yonyou.brigade.order_center.util.OrderConvert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xuexmf@yonyou.com
 * @since 2018-08-08
 */
@Service
public class BriHotelRoomServiceImpl extends ServiceImpl<BriHotelRoomMapper, BriHotelRoom> implements IBriHotelRoomService {

    @Autowired
    private BriHotelRoomMapper briHotelRoomMapper;

    @Override
    public List<BriHotelRoom> getBriHotelRoom(String orderNo) {
        return briHotelRoomMapper.getBriHotelRoomList(orderNo);
    }

    @Override
    public Map getBriHotelRoomDTOs(Map params) {
        Map queryParams = params;
        queryParams.put("tenant",params.get("tenantId"));
        int pageSize=Integer.parseInt(params.get("pageSize").toString());
        int current=Integer.parseInt(params.get("current").toString());
        if(pageSize<=0) {
            pageSize=10;
        }
        if(current<=0) {
            current=1;
        }
        int begin=(current-1)*pageSize;
        queryParams.put("begin",begin);
        queryParams.put("pageSize",pageSize);
        Map result=new HashMap();
        try {
            List<BriHotelRoomDTO> briHotelRoomDTOS = briHotelRoomMapper.getBriHotelRoomDTOs(queryParams);
            int total=briHotelRoomMapper.getBriHotelRoomDTOsCount(queryParams);
            // 展示时将订单总金额平均分配
            if (!CollectionUtils.isEmpty(briHotelRoomDTOS)) {
                for (BriHotelRoomDTO briHotelRoomDTO : briHotelRoomDTOS) {
                    String orderNO = briHotelRoomDTO.getOrderNO();
                    int i = 0;
                    for (BriHotelRoomDTO hotelRoomDTO : briHotelRoomDTOS) {
                        if (!StringUtils.isEmpty(orderNO) && orderNO.equals(hotelRoomDTO.getOrderNO())) {
                            i++;
                        }
                    }
                    if (i > 1) {
                        briHotelRoomDTO.setPaymentPrice(String.valueOf(new BigDecimal(briHotelRoomDTO.getPaymentPrice()).divide(new BigDecimal(i),4,BigDecimal.ROUND_HALF_UP)));
                    }
                }
            }
            List<BriHotelRoomDTO> briHotelRoomDTOList =perfectHotelRoomDTOs(briHotelRoomDTOS);
            result.put("information", briHotelRoomDTOList);
            result.put("code","0");
            JSONObject pagination=new JSONObject();
            pagination.put("total",total);
            pagination.put("pageSize",pageSize);
            pagination.put("current",current);
            result.put("pagination",pagination);
        }
        catch (Exception e)
        {
            result.put("code","100");
            result.put("information","操作失败");
            e.printStackTrace();
        }

        return result;
    }

    private List<BriHotelRoomDTO> perfectHotelRoomDTOs(List<BriHotelRoomDTO> hotelRoomDTOS)
    {
        List<BriHotelRoomDTO> hotelRoomDTOList=new ArrayList<>();
        if(hotelRoomDTOS==null)
        {
            return  hotelRoomDTOS;
        }
        else
        {
            for(int i=0;i<hotelRoomDTOS.size();i++)
            {
                hotelRoomDTOList.add(OrderConvert.perfactHotelRoomDTO(hotelRoomDTOS.get(i)));
            }
        }
        return  hotelRoomDTOList;
    }

}
